package com.yb.xczx.cloud.auth.oauth2.handler.mybatis;

import org.springframework.security.oauth2.core.ClientAuthenticationMethod;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/21 15:37
 * FileName: XczxClientAuthenticationMethodSetTypeHandler
 * Description:
 */
public class XczxClientAuthenticationMethodSetTypeHandler extends XczxSetTypeHandler<ClientAuthenticationMethod> {
    @Override
    protected ClientAuthenticationMethod getResult(String resultStr) {
        return resolveClientAuthenticationMethod(resultStr);
    }

    @Override
    protected String itemToString(ClientAuthenticationMethod clientAuthenticationMethod) {
        return clientAuthenticationMethod.getValue();
    }

    private static ClientAuthenticationMethod resolveClientAuthenticationMethod(String clientAuthenticationMethod) {
        if (ClientAuthenticationMethod.CLIENT_SECRET_BASIC.getValue().equals(clientAuthenticationMethod)) {
            return ClientAuthenticationMethod.CLIENT_SECRET_BASIC;
        } else if (ClientAuthenticationMethod.CLIENT_SECRET_POST.getValue().equals(clientAuthenticationMethod)) {
            return ClientAuthenticationMethod.CLIENT_SECRET_POST;
        } else if (ClientAuthenticationMethod.NONE.getValue().equals(clientAuthenticationMethod)) {
            return ClientAuthenticationMethod.NONE;
        }
        return new ClientAuthenticationMethod(clientAuthenticationMethod);        // Custom client authentication method
    }
}
